﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using com.hisun.iposm.net;
using System.Xml;
public partial class shoujiqianbao_webPay_test : System.Web.UI.Page
{
    public static log4net.ILog log = log4net.LogManager.GetLogger(typeof(shoujiqianbao_webPay_test));
    protected void Page_Load(object sender, EventArgs e)
    {
        GlobalParam param = GlobalParam.getInstance();
        if (String.IsNullOrEmpty(param.merchantId))
        {
            XmlDocument document = new XmlDocument();
            try
            {
                string _codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                _codeBase = _codeBase.Substring(8, _codeBase.Length - 8);
                int pos = _codeBase.LastIndexOf("/");
                if (pos > 0)
                {
                    _codeBase = _codeBase.Substring(0, pos + 1);
                }
                else
                {
                    _codeBase = "";
                }
                log.Info("conf base path=" + _codeBase);
                document.Load(_codeBase + "MerchantInfo.xml");
            }
            catch (Exception e1)
            {
                log.Error(e1);
                log.Error(e1.Message);
            }
            log.Info("merchantId == null");
        }

        if (!IsPostBack)
        {

            //页面初始化
            //公共信息
            GetOrderInfo();

            callbackUrl = System.Configuration.ConfigurationManager.AppSettings["callbackUrl"];
            ipAddress = System.Configuration.ConfigurationManager.AppSettings["ipAddress"];
            notifyUrl = System.Configuration.ConfigurationManager.AppSettings["notifyUrl"];
            notifyEmail = System.Configuration.ConfigurationManager.AppSettings["notifyEmail"];
            notifyMobile = System.Configuration.ConfigurationManager.AppSettings["notifyMobile"];

            log.Info("webPay start");
            //获取令牌
            MerchantTokenPayReq req = new MerchantTokenPayReq();
            req.amount =0.01;
            //req.amount = p_amount;
            req.callbackUrl = callbackUrl;
            req.currency = IPosMUtil.convertCurFromStr("CNY");
            req.deliverFlag = false; //送货标志
            req.invoiceFlag = false;//发票标志
            req.ipAddress = ipAddress;
            req.notifyEmail = notifyEmail;
            req.notifyMobile = notifyMobile;
            req.notifyUrl = notifyUrl;
            req.orderDate = orderdate;
            req.orderId = OrderId;
            req.period = 2;
            req.periodUnit = IPosMUtil.convertPeriodFromInt("2");
            req.productDesc = "非常好";
            req.productId = "emoney";
            req.productName = "益盟软件";
            req.reserved = "reservedreservedreserved";

            MerchantTokenPayRes res = null;
            int type = Convert.ToInt32(0);//目前只支持这种支付
            try
            {

                switch (type)
                {
                    case 0:
                        res = HiSunTransEngine.tokenPay(req);
                        break;
                    case 1:
                        res = HiSunTransEngine.tokenPad(req);
                        break;
                    case 2:
                        res = HiSunTransEngine.tokenB2C(req);
                        break;
                    case 3:
                        res = HiSunTransEngine.tokenC2C(req);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e1)
            {
                log.Error(e1.StackTrace);
                errMsg.Text = e1.Message;
                return;
            }

            if (res == null)
            {
                errMsg.Text = "获取令牌出错！";
                return;
            }

            //Response.Redirect("http://127.0.0.1:8080/demoMerchantNew/backUrl.jsp?sessionId=" + res.sessionid);
            String url = System.Configuration.ConfigurationManager.AppSettings["tokenRedirectUrl"];
            Response.Redirect(url + "?SESSIONID=" + res.sessionid);
            //Response.Write("＜script＞alert('dddd');＜/script＞");


        }
    }


    pay.BLL.OrderBLL orderbll = new pay.BLL.OrderBLL();
    string OrderId = "";
    string payname = "";
    double p_amount = 0;
    int allnum = 0;
    string orderdate = "";
    string desc = "";
    string callbackUrl = "";
    string ipAddress = "";
    string notifyUrl = "";
    string notifyEmail = "";
    string notifyMobile = "";

    //获取下单的信息
    public void GetOrderInfo()
    {

        if (Request["ordernum"] == null)
        {
            return;
        }
        string ordernumber = Request["ordernum"];
        pay.Entity.Web_Order order = orderbll.GetOrderByOrderNumber(ordernumber);
        OrderId = order.Ordernumber.Trim();
        orderdate = order.Orderdate.Value.ToString("yyyyMMdd").Trim();
        p_amount = Convert.ToDouble(order.Allmoney);
        //desc =(order.Remark == "" ? "非常好" : order.Remark);
        // TotalPrice = Convert.ToInt32(order.Allmoney.ToString().Trim());
        //payname = order.Username;
        //allnum = Convert.ToInt32(order.Allnum);

    }
}